#!/bin/bash

source ./TestSuite.config

LOGFILE=$TestSuiteLocation/Results/tslog
if [ "$1" = "y" ] ; then
	DONTASK='y'
fi

if [ "$DONTASK" != "y" ] ; then
	echo "This setup script is meant to setup all necessary components on a new machine"
	echo "for testing. It will modify the system by installing packages and setup certificates"
	echo "in /etc/grid-security."
	read -p "Are you sure you want to continue? (y/n)" USER_REPLY
	if [ "$USER_REPLY" != "y" ] ; then
		exit 0;
	fi
	echo ""
fi

echo -n "Checking for priviledged user... "
if [ `whoami` != "root" ] ; then 
	echo "no"
	echo "This Setup script will work only as root"
	exit 1
else
	echo "yes"
fi

# Installation of basic packages
$TestSuiteLocation/bin/pkg_inst.sh globus-proxy-utils python-pip  &>>  $LOGFILE
/usr/bin/easy_install robotframework &>> $LOGFILE

if [ "$UseTestSuiteGSI" = "yes" ] ; then
	if [ -d /etc/grid-security -a "$DONTASK" != 'y' ] ; then
		echo "Warning: /etc/grid-security folder found. This setup script will move it to"
		echo "/etc/grid-security.bak and setup the generated grid-security folder there."
		echo "The generated grid-security folder should ONLY be used for testing purposes"
		echo "and should be removed using the provided uninstall script."
		read -p "Are you sure you want to continue? (y/n)" USER_REPLY
		if [ "$USER_REPLY" != "y" ] ; then
			exit 0;
		fi
	fi
	export GRID_SECURITY_DIR="$TestSuiteLocation/IGE-TestSuite-Certs/grid-security"
	export X509_CERT_DIR="$TestSuiteLocation/IGE-TestSuite-Certs/grid-security/certificates"
	echo -n "Installing globus simpleCA tools..."
	$TestSuiteLocation/bin/pkg_inst.sh globus-simple-ca globus-gsi-cert-utils-progs  &>>  $LOGFILE
	if [ $? -ne 0 ] ; then
		echo "failed. Please see $LOGFILE for details."
		exit 1
	else
		echo "done."
	fi
	
	echo -n "Setting up certificates..."
	$TestSuiteLocation/bin/setup_gsissh_cert.sh >> $LOGFILE
	if [ $? -ne 0 ] ; then
		echo "failed. Please see $LOGFILE for details."
		exit 1
	else
		echo "done."
	fi

	if [ "$CreateTestSuiteUsers" = "yes" ] ; then
		echo "Creating test users"
		$TestSuiteLocation/bin/create_test_user_cert.sh -c -u "$TS_TESTUSER_ALT" &>> $LOGFILE
	fi
	echo -n "Moving /etc/grid-security to /etc/grid-security.bak ..."
	mv /etc/grid-security /etc/grid-security.bak &>> $LOGFILE
	cp -R $TestSuiteLocation/IGE-TestSuite-Certs/grid-security /etc &>> $LOGFILE
	if [ $? -ne 0 ] ; then
		echo "failed. Please see $LOGFILE for details."
		exit 1
	else
		echo "done."
	fi
	unset GRID_SECURITY_DIR
	unset X509_CERT_DIR
fi


if [ -n "$SetupServices" ] ; then
	echo "Installing Services"
	echo "-------------------"
	BACKIFS=$IFS	
	IFS=','
	for service in $SetupServices ; do
	case "$service" in
		gsissh|gsiopenssh)
			echo -n "Installing GSI-openSSH... "
			$TestSuiteLocation/bin/pkg_inst.sh gsi-openssh-clients gsi-openssh-server &>> $LOGFILE
			if [ $? -ne 0 ] ; then
				echo "failed. Please see $LOGFILE for details."
				exit 1
			else
				echo "done."
			fi

			# Setup of init scripts
			sed -i "s/#Port 22/Port 2222/" /etc/gsissh/sshd_config &>> $LOGFILE
			echo -n "Starting gsissh server... "
			/etc/init.d/gsisshd start &>> $LOGFILE
			if [ $? -ne 0 ] ; then
				echo "failed. Please see $LOGFILE for details."
				exit 1
			else
				echo "done."
			fi

			;;
		gridftp)
			echo -n "Installing GridFTP... "
			$TestSuiteLocation/bin/pkg_inst.sh globus-gridftp-server globus-gridftp-server-control globus-gridftp-server-progs globus-gass-copy-progs &>> $LOGFILE
			if [ $? -ne 0 ] ; then
				echo "failed. Please see $LOGFILE for details."
				exit 1
			else
				echo "done."
			fi

			# Setup of init scripts
			echo -n "Starting gridftp server... "
			/etc/init.d/globus-gridftp-server start &>> $LOGFILE
			if [ $? -ne 0 ] ; then
				echo "failed. Please see $LOGFILE for details."
				exit 1
			else
				echo "done."
			fi
			;;
		gram)
			echo -n "Installing GRAM... "
			$TestSuiteLocation/bin/pkg_inst.sh globus-gatekeeper globus-gram-client-tools globus-gram-job-manager-fork-setup-poll &>> $LOGFILE
			if [ $? -ne 0 ] ; then
				echo "failed. Please see $LOGFILE for details."
				exit 1
			fi

			# Setup of init scripts
			globus-gatekeeper-admin -e jobmanager-fork-poll	&>> $LOGFILE
			if [ $? -ne 0 ] ; then
				echo "failed. Please see $LOGFILE for details."
				exit 1
			else
				echo "done."
			fi

			echo -n "Starting Gatekeeper... "
			/etc/init.d/globus-gatekeeper start &>> $LOGFILE
			if [ $? -ne 0 ] ; then
				echo "failed. Please see $LOGFILE for details."
				exit 1
			else
				echo "done."
			fi
			;;
		*)
			exit 1
	esac
	done
elif [ -n "$SetupClients" ] ; then
	BACKIFS=$IFS	
	IFS=','
	for service in $SetupClients; do
	case "$service" in
		gsissh|gsiopenssh)
			$TestSuiteLocation/bin/pkg_inst.sh gsi-openssh-clients &>> $LOGFILE
			;;
		gridftp)
			$TestSuiteLocation/bin/pkg_inst.sh globus-gass-copy-progs &>> $LOGFILE
			;;

		gram)
			$TestSuiteLocation/bin/pkg_inst.sh globus-gram-client-tools &>> $LOGFILE
			;; 
		*)
			exit 1
	esac
	done
fi
